package com.CST.service.impl.zx;

import com.CST.common.api.ResultObject;
import com.CST.entity.cb.BdSysUser;
import com.CST.entity.zx.ZxExpenseType;
import com.CST.mapper.cst.zx.ZxExpenseTypeMapper;
import com.CST.service.cb.IBdSysUserService;
import com.CST.service.zx.IZxDataentryService;
import com.CST.service.zx.IZxExpenseTypeService;
import com.CST.util.ExcelTransfer;
import com.CST.util.HttpUtil;
import com.CST.util.PageHelperTool;
import com.CST.vo.zx.ZxExpenseTypeVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2023-03-03
 */
@Service
public class ZxExpenseTypeServiceImpl extends ServiceImpl<ZxExpenseTypeMapper, ZxExpenseType> implements IZxExpenseTypeService {

    @Resource
    private ZxExpenseTypeMapper zxExpenseTypeMapper;
    @Resource
    private IZxDataentryService zxDataentryService;
    @Resource
    private IBdSysUserService bdSysUserService;
    @Resource
    private ExcelTransfer<ZxExpenseType> excelTransfer;

    @Override
    public ResultObject myPage(ZxExpenseType zxExpenseType) {
        List<ZxExpenseTypeVO> zxExpenseTypeVOS = zxExpenseTypeMapper.myPage(zxExpenseType);
        return ResultObject.successReturn(PageHelperTool.excutePageInfo(zxExpenseTypeVOS, zxExpenseType.getPageNo(), zxExpenseType.getPageSize()));
    }

    @Override
    public boolean saveOrUpdateZxExpense(ZxExpenseType zxExpenseType) {
        String userId = HttpUtil.getRequestHeaderInfo("userId");
        BdSysUser user = bdSysUserService.getUserById(userId);
        LocalDateTime now = LocalDateTime.now();
        if (zxExpenseType.getId() != null) {
            zxExpenseType.setUpdateuserId(user.getUserName());
            zxExpenseType.setUpdatetime(now);
            return this.updateById(zxExpenseType);
        }
        zxExpenseType.setCreateuserId(user.getUserName());
        zxExpenseType.setCreatetime(now);
        return this.save(zxExpenseType);
    }

    @Override
    public void download(HttpServletResponse response, ZxExpenseType zxExpenseType) {
        List<ZxExpenseTypeVO> zxExpenseTypeVOS = zxExpenseTypeMapper.myPage(zxExpenseType);
        List<ZxExpenseType> list = new ArrayList<>();
        list = zxExpenseTypeVOS.stream().map(zxExpenseTypeVO -> {
            ZxExpenseType type = new ZxExpenseType();
            BeanUtils.copyProperties(zxExpenseTypeVO,type);
            return type;
        }).collect(Collectors.toList());
        ExcelTransfer<ZxExpenseType> excelTransfer = new ExcelTransfer<>();
        try {
            excelTransfer.exportExcel(response,list,"科目关系对照","科目关系对照明细", ZxExpenseType.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveFile(MultipartFile file) {
        try {
//            ConvertList<ZxExpenseType> convertList = list -> {
//                list = list.stream().map(zxExpenseType -> {
//                    zxExpenseType.setCreatetime(LocalDateTime.now());
//                    return zxExpenseType;
//                }).collect(Collectors.toList());
//                return list;
//            };
            excelTransfer.importExcel(file, this,ZxExpenseType.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> getDistinctAccount() {
        return zxExpenseTypeMapper.getDistinctAccount();
    }
}
